package com.crm.jdy.controller.es;

import com.beust.jcommander.internal.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@Api(tags = "es", description ="ES全文检索")
@RestController
@RequestMapping("/es")
public class MyController {

    @Qualifier("highLevelClient")
    @Autowired
    RestHighLevelClient highLevelClient;

    @GetMapping("/searchadd")
    @ApiOperation("根据id查询ES对应的数据")
    public void  searchadd() throws IOException {

        CreateIndexRequest index = new CreateIndexRequest("index_name");
        Map<String,Object> properties = Maps.newHashMap();
        Map<String,Object> propertie = Maps.newHashMap();
        propertie.put("type","text");
        propertie.put("index",true);
        propertie.put("analyzer","ik_max_word");
        properties.put("field_name",propertie);
        XContentBuilder builder = JsonXContent.contentBuilder();
        builder.startObject()
                .startObject("mappings")
                .startObject("index_name")
                .field("properties",properties)
                .endObject()
                .endObject()
                .startObject("settings")
                .field("number_of_shards",3)
                .field("number_of_replicas",1)
                .endObject()
                .endObject();
        index.source(builder);
        CreateIndexResponse createIndexResponse = highLevelClient.indices().create(index, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
        System.out.println("-----------");

    }

    @GetMapping("/GetIndex")
    @ApiOperation("根据id判断是否存在ES")
    public boolean GetIndex(String indexName) {
        GetIndexRequest request = new GetIndexRequest();
        request.indices(indexName);
        try {
            return highLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }


    @GetMapping("/deleteIndex")
    @ApiOperation("根据id删除ES")
    public boolean deleteIndex(String indexName) {
        DeleteIndexRequest index = new DeleteIndexRequest(indexName);
        try {
            highLevelClient.indices().delete(index);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }


//    @Autowired
//    private MyService service;
//
//    @PostMapping("/search")
//    @ApiOperation("根据id查询ES对应的数据")
//    public JSONObject getDataById(@RequestBody SearchParam param) {
//        return service.getDataById(param);
//    }
//
//    @PostMapping("/add")
//    @ApiOperation("往ES里插入数据")
//    public ResponseEntity add(@RequestBody SearchParam param) {
//        return service.add(param);
//    }
//
//    @PostMapping("/update")
//    @ApiOperation("根据id更新文档内容")
//    public ResponseEntity update(@RequestBody SearchParam param) {
//        return service.update(param);
//    }
//
//    @PostMapping("/delete")
//    @ApiOperation("根据id更新文档内容")
//    public ResponseEntity delete(@RequestBody SearchParam param) {
//        return service.delete(param);
//    }


}
